package cn.keniykey.mc.viewlibrary.container;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import cn.keniykey.mc.viewlibrary.ViewLibraryMain;
import cn.keniykey.mc.viewlibrary.channel.AbPacketReceiveHandler;
import cn.keniykey.mc.viewlibrary.container.api.Container;
import cn.keniykey.mc.viewlibrary.container.api.ContainerAnvil;
import cn.keniykey.mc.viewlibrary.container.api.ContainerAnvil.IContainerAnvilItemNameHandler;
import cn.keniykey.mc.viewlibrary.container.api.ContainerClickReport;
import cn.keniykey.mc.viewlibrary.container.api.EClickType;
import cn.keniykey.mc.viewlibrary.container.api.IContainerClickHandler;
import cn.keniykey.mc.viewlibrary.packet.AbPacketUtil;
import cn.keniykey.mc.viewlibrary.packet.api.AbPacket;
import cn.keniykey.mc.viewlibrary.packet.api.AbWindowPacket;
import cn.keniykey.mc.viewlibrary.packet.api.PacketItemName;
import cn.keniykey.mc.viewlibrary.packet.api.PacketWindowClick;
import cn.keniykey.mc.viewlibrary.packet.api.PacketWindowClose;
import cn.keniykey.mc.viewlibrary.packet.api.PacketWindowCloseDispatch;
import cn.keniykey.mc.viewlibrary.packet.api.PacketWindowSetSlotDispatch;

public class ContainerPacketReceiveHandler extends AbPacketReceiveHandler {

	ContainerPacketReceiveHandler() {
	}

	@SuppressWarnings("deprecation")
	@Override
	protected boolean onServerReceivePacket(final Object nmspacket) {
		final Player player = this.getPlayer();
		final Container container = Container.getViewingContainer(player);
		if(container == null) return false;
		final AbPacket packet = AbPacketUtil.getUtil().coverPacket(nmspacket, AbWindowPacket.WINDOW_ID);
		if(packet == null) return false;
		if(packet instanceof PacketWindowClose) {
			container.close(player, false, false);
			Bukkit.getScheduler().runTask(ViewLibraryMain.getPlugin(), ()-> {
				//防止关闭菜单后, 动态标题频率过快出现的卡假容器（来自TrMenu）
				if(!Container.hasViewingContainer(player)) player.updateInventory();
			});
			Bukkit.getScheduler().runTaskLaterAsynchronously(ViewLibraryMain.getPlugin(), ()-> {
				if(!Container.hasViewingContainer(player)) new PacketWindowCloseDispatch().sendPacket(player);
			}, 4l);
			return true;
		}
		if(packet instanceof PacketWindowClick) {
			final int slot = ((PacketWindowClick)packet).getSlot();
			final int button = ((PacketWindowClick)packet).getButton();
			final EClickType clicktype;
			final Object mode = ((PacketWindowClick)packet).getMode();
			if(mode instanceof String) {
				clicktype = EClickType.getMatch((String)mode, button, slot);
			}else if(mode instanceof Integer) {
				clicktype = EClickType.getMatch((int)mode, button, slot);
			}else {
				return false;
			}
			if(clicktype == null) return false;
			final IContainerClickHandler handler = container.getIContainerClickHandler();
			if(handler != null) {
				final ContainerClickReport report = new ContainerClickReport(player, container, clicktype, slot);
				try {
					handler.onContainerClickResponse(player, report);
				}catch(Exception ex) {
					ex.printStackTrace();
				}
				if(slot >= 0) {
					final ItemStack curitem = ContainerClickReport.getItemStackFromSlot(player, container, slot);
					if(clicktype.isNumberKeyClick() && button >= 0) {
						final int numberslot = (container.getContainerSize() + 27 + button);
						if(numberslot != slot) {
							new PacketWindowSetSlotDispatch(slot, curitem).sendPacket(player);
							new PacketWindowSetSlotDispatch(numberslot, player.getInventory().getItem(button)).sendPacket(player);
						}
					}else {
						if(curitem != null) {
							new PacketWindowSetSlotDispatch(-1, -1, null).sendPacket(player);
							new PacketWindowSetSlotDispatch(slot, curitem).sendPacket(player);
						}
					}
				}
			}
			return true;
		}
		if(packet instanceof PacketItemName) {
			if(!(container instanceof ContainerAnvil)) return false;
			final IContainerAnvilItemNameHandler handler = ((ContainerAnvil)container).getContainerAnvilItemNameHandler();
			if(handler != null) {
				try {
					handler.onAnvilItemNameResponse(player, (ContainerAnvil)container, ((PacketItemName)packet).getName());
				}catch(Exception ex) {
					ex.printStackTrace();
				}
			}
			return true;
		}
		return false;
	}
}